package com.example.myapplication;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.myapplication.anim.TestActivity2;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Queue;

@RequiresApi(api = Build.VERSION_CODES.O)
public class MainActivity extends AppCompatActivity {

    private static final Base64.Decoder DECODE_64 = Base64.getDecoder();
    private static final Base64.Encoder ENCODE_64 = Base64.getEncoder();
    ImageView iv;
    private int mItemWidth = 100;
    private int mItemHeight = 100;
    List<Drawable> mList = new ArrayList<>();
    Queue<ImageView> mIV = new ArrayDeque<>();
    volatile List<ImageView> mListView = new ArrayList<>();
    LinearLayout layout;
    Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = findViewById(R.id.iv1);
        layout = findViewById(R.id.layout);
        initData();

    }

    private void initData() {
        mList.add(getResources().getDrawable(R.drawable.icon_battery));
        mList.add(getResources().getDrawable(R.drawable.icon_battery));
        mList.add(getResources().getDrawable(R.drawable.icon_battery));
        mList.add(getResources().getDrawable(R.drawable.icon_battery));
        mList.add(getResources().getDrawable(R.drawable.icon_battery));
        mList.add(getResources().getDrawable(R.drawable.icon_battery));
        mList.add(getResources().getDrawable(R.drawable.icon_battery));
    }



    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.butt:
//                startAnimator();
//                ImageView imageView = new ImageView(this);
//                imageView.setLayoutParams(new RelativeLayout.LayoutParams((int) mItemWidth, (int) mItemHeight));
//                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//                imageView.setImageResource(R.drawable.icon_battery);
//                imageView.setTranslationX(0);
//                layout.addView(imageView);
//                startAnimatorSet(imageView);

//                Intent intent = new Intent(this,AnimatorTest.class);
                Intent intent = new Intent(this, TestActivity2.class);

                startActivity(intent);
//                Intent intent1 = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
//                intent1.setData(Uri.fromParts("package", getPackageName(), null));
//                startActivity(intent1);
//                Log.i("llll", "startAnimator: "+imageView.getTranslationX());

        }
    }

    private void startAnimator() {
        for (int i = 0 ; i<mList.size(); i++){
            ItemCleanView itemCleanView = createCleanItem(mList.get(i), i);
            Log.i("llll", "startAnimator: "+itemCleanView.mImageView.getTranslationX());
            layout.addView(itemCleanView.mImageView);
            mListView.add(itemCleanView.mImageView);
        }
        for (int i = 0; i < mListView.size(); i++){

//            startAnimatorSet(mListView.get(i));
        }
    }
    private  class ItemCleanView {
        ImageView mImageView;
        float y;
        float speedY;

        public ItemCleanView(ImageView imageView, float x) {
            mImageView = imageView;
            speedY = 50;
            mImageView.setTranslationX(x);
        }
    }
    private ItemCleanView createCleanItem(Drawable drawable, int i) {
        ImageView imageView = new ImageView(this);
        imageView.setLayoutParams(new RelativeLayout.LayoutParams((int) mItemWidth, (int) mItemHeight));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageDrawable(drawable);
        return new ItemCleanView(imageView, getScreenWidth()/2-i*mItemWidth);
    }
    public void startAnimatorSet(ImageView imageView){
        ObjectAnimator animator1 = (ObjectAnimator) getAnimator1(imageView);
        ObjectAnimator animator2 = (ObjectAnimator) getAnimator2(imageView);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playSequentially(animator1,animator2);

        animatorSet.start();

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                animatorSet.cancel();
            }
        },2000);
    }
//最右边的动画：
    public Animator getAnimator1(ImageView imageView){

        ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "translationX", getScreenWidth()/2, getScreenWidth());
        animator.setInterpolator(new AnticipateOvershootInterpolator());
        animator.setDuration(750);
        animator.setRepeatMode(ValueAnimator.REVERSE);
        Log.i("llll", "startAnimator: "+imageView.getTranslationX());

        return animator;
    }
    //中间两个
    public Animator getAnimator2(ImageView imageView){
        Log.i("llll", "startAnimator: "+imageView.getTranslationX());
        ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "translationX", imageView.getTranslationX(), imageView.getTranslationX()+imageView.getWidth());
        animator.setInterpolator(new AnticipateOvershootInterpolator());
        animator.setDuration(750);
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.setStartDelay(500);
        return animator;
    }
    //最左边：
    public Animator getAnimator3(ImageView imageView){
        Log.i("llll", "startAnimator: "+imageView.getTranslationX());
        ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "translationX", -20, 0);
        animator.setInterpolator(new AnticipateOvershootInterpolator());
        animator.setDuration(750);
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.setStartDelay(500);
        return animator;
    }
    public int getScreenWidth(){
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int screenWidth = metrics.widthPixels;//获取屏幕宽度
        return screenWidth;
    }

}